<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="org.json.simple.JSONObject"%>
<%@ page import="org.json.simple.JSONArray"%>
<%@ page import="java.net.URLEncoder"%>

<%
	
	String mcode=request.getParameter("mcode");
	String fmcode=request.getParameter("mcode");
	//String mcode="M1";
	//String fmcode="M1";
	//String omcode="M2";
	
	String url="jdbc:oracle:thin:@192.168.0.18:1521:orcl";
	String user="book";
	String password="book";
	
	String sql1=null;
	String sql2=null;
	String sql3=null;
	String sql4=null;
	String sql5=null;
	
	String nick="";
	String name="";
	String picode="";
	String contentcount="";
	String followcount="";
	String followingcount="";
	String heartcount="";
	
	Connection conn=null;
	ResultSet rs1=null;
	ResultSet rs2=null;
	ResultSet rs3=null;
	ResultSet rs4=null;
	ResultSet rs5=null;
	PreparedStatement stmt1=null;
	PreparedStatement stmt2=null;
	PreparedStatement stmt3=null;
	PreparedStatement stmt4=null;
	PreparedStatement stmt5=null;
	
	try{
		Class.forName("oracle.jdbc.driver.OracleDriver");
		//out.println("드라이버 로딩 성공");
		conn=DriverManager.getConnection(url, user, password);
		//out.println("접속 성공");
		
		sql1="SELECT member.mname, MEMBER.MNICKNAME, member.picode FROM MEMBER WHERE MEMBER.MCODE=?";
		sql2="SELECT COUNT(MCODE) FROM BOARD WHERE MCODE=?";
		sql3="SELECT COUNT(FMCODE) FROM FOLLOW WHERE FMCODE=?";
		sql4="SELECT COUNT(MCODE) FROM FOLLOW WHERE MCODE=?";
		sql5="SELECT MCODE, SUM(HPLUS)-SUM(HMINUS) as TOTAL FROM HEART WHERE MCODE=? GROUP BY ROLLUP(MCODE)";
		
		stmt1=conn.prepareStatement(sql1);
		stmt2=conn.prepareStatement(sql2);
		stmt3=conn.prepareStatement(sql3);
		stmt4=conn.prepareStatement(sql4);
		stmt5=conn.prepareStatement(sql5);
		
		stmt1.setString(1, mcode);
		rs1 = stmt1.executeQuery();
		
		stmt2.setString(1, mcode);
		rs2 = stmt2.executeQuery();
		
		stmt3.setString(1, fmcode);
		rs3 = stmt3.executeQuery();
		
		stmt4.setString(1, mcode);
		rs4 = stmt4.executeQuery();
		
		stmt5.setString(1, mcode);
		rs5 = stmt5.executeQuery();
		
		while(rs1.next()){
			nick=rs1.getString("mnickname");
			name=rs1.getString("mname");
			picode=rs1.getString("picode");
		}
		
		while(rs2.next()){
			contentcount=rs2.getString("count(mcode)");
		}
		
		while(rs3.next()){
			followcount=rs3.getString("count(fmcode)");
		}
		
		while(rs4.next()){
			followingcount=rs4.getString("count(mcode)");
		}
		
		while(rs5.next()){
			heartcount=rs5.getString("total");
		}
		
	}catch(Exception e){
		out.println("접속 실패");
		e.printStackTrace();
	}finally{
		if(stmt1!=null) stmt1.close();
		if(stmt2!=null) stmt2.close();
		if(stmt3!=null) stmt3.close();
		if(stmt4!=null) stmt4.close();
		if(stmt5!=null) stmt5.close();
		if(conn!=null) conn.close();
	}
	
%>
{"nick" : "<%= nick %>",
 "contentcount" : "<%= contentcount %>",
 "followcount" : "<%= followcount %>",
 "followingcount" : "<%= followingcount %>",
 "heartcount" : "<%= heartcount %>",
 "name" : "<%= name %>",
 "picode" : "<%= picode %>" }
